package bb.lanxing.adapter.sport;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.model.database.PacePoint;
import bb.lanxing.util.DateUtil;

public class SportRecordPaceAdapter extends RecyclerView.Adapter<SportRecordPaceAdapter.ViewHolder> {
    private Context mContext;
    private List<PacePoint> pacePointList;
    private List<Long> durationList = new ArrayList<>();
    private long maxDuration = 0;
    private int fastPosition = 0;

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView index;
        RelativeLayout rectangle;
        TextView pace;
        TextView fast;
        TextView time;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            index = itemView.findViewById(R.id.index);
            rectangle = itemView.findViewById(R.id.rectangle);
            pace = itemView.findViewById(R.id.pace);
            fast = itemView.findViewById(R.id.fast);
            time = itemView.findViewById(R.id.time);
        }
    }

    public SportRecordPaceAdapter(List<PacePoint> pacePointList) {
        this.pacePointList = pacePointList;
        long sum = 0;
        double maxSpeed = 0;
        for (int i = 0; i < pacePointList.size(); i++) {
            PacePoint pacePoint = pacePointList.get(i);
            sum += pacePoint.getDuration();
            durationList.add(sum);
            if (pacePoint.getMaxSpeed() > maxSpeed) {
                maxSpeed = pacePoint.getMaxSpeed();
                fastPosition = i;
            }
            if (pacePoint.getDuration() > maxDuration) {
                maxDuration = pacePoint.getDuration();
            }
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sport_record_pace, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.index.setText((position + 1) + "");
        PacePoint pacePoint = pacePointList.get(position);
        ViewGroup.LayoutParams layoutParams = holder.rectangle.getLayoutParams();
        layoutParams.width = (int) (layoutParams.width * (0.7 + 0.3 * pacePoint.getDuration() / maxDuration));
        holder.rectangle.setLayoutParams(layoutParams);
        Drawable drawable = holder.rectangle.getBackground();
        if (position == fastPosition) {
            drawable.setColorFilter(mContext.getColor(R.color.sport_record_pace_bg1), PorterDuff.Mode.SRC_ATOP);
            holder.fast.setVisibility(View.VISIBLE);
        } else {
            drawable.setColorFilter(mContext.getColor(R.color.sport_record_pace_bg), PorterDuff.Mode.SRC_ATOP);
        }
        holder.rectangle.setBackground(drawable);
        holder.pace.setText(pacePoint.getPaceFormat());

        holder.time.setText(DateUtil.format(durationList.get(position) * 1000, 2));
    }

    @Override
    public int getItemCount() {
        return pacePointList.size();
    }
}